﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

/// <summary>
/// Summary description for accountDAO
/// </summary>
public class accountDAO:DataProvider
{
    public static string EncryptionMD5(string sinput)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(sinput);
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] hash = md5.ComputeHash(bytes);
        StringBuilder sb = new StringBuilder();
        foreach (byte b in hash)
        {
            sb.Append(string.Format("{0:X2}", b)); 
        }
        return sb.ToString();
    }
    public accountDTO getAccount(string _account, string _password)
    {
        accountDTO account = new accountDTO();
        OleDbConnection connect = null;
        try
        {
            connect = Connect();
            string strCommand = "SELECT * FROM ACCOUNT WHERE account = @account AND password=@password ";
            OleDbCommand command = new OleDbCommand(strCommand, connect);
            
            OleDbParameter param;
            param = new OleDbParameter("@account", OleDbType.VarChar);
            param.Value =_account;
            command.Parameters.Add(param);
            param = new OleDbParameter("@password", OleDbType.VarChar);
            param.Value =_password;
            command.Parameters.Add(param);

            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                account.ID = reader.GetInt32(0);
                account.Account = reader.GetString(1);
            }
        }
        catch (Exception ex)
        {
            account = null;
        }
        finally
        {
            if (connect != null && connect.State == System.Data.ConnectionState.Open)
                connect.Close();
        }
        return account;
    }
    public bool insertAccount(accountDTO account)
    {
        bool result = true; 
        OleDbConnection connect = null;
        try
        {
            connect = Connect();
            string strCommand = "INSERT INTO ACCOUNT(account, password) VALUES(@account, @password)";
            OleDbCommand command = new OleDbCommand(strCommand, connect);
            OleDbDataReader reader = command.ExecuteReader();

            OleDbParameter param;
            param = new OleDbParameter("@account", OleDbType.VarChar);
            param.Value = account.Account;
            command.Parameters.Add(param);
            param = new OleDbParameter("@password", OleDbType.VarChar);
            param.Value = account.Password;
            command.Parameters.Add(param);

            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            result = false;
        }
        finally
        {
            if (connect != null && connect.State == System.Data.ConnectionState.Open)
                connect.Close();
        }
        return result;
    }
    public bool deleteAccount(accountDTO account)
    {
        bool result = true;
        OleDbConnection connect = null;
        try
        {
            connect = Connect();
            string strCommand = "DELETE FROM ACCOUNT WHERE account = @account";
            OleDbCommand command = new OleDbCommand(strCommand, connect);
            OleDbDataReader reader = command.ExecuteReader();

            OleDbParameter param;
            param = new OleDbParameter("@account", OleDbType.VarChar);
            param.Value = account.Account;
            command.Parameters.Add(param);

            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            result = false;
        }
        finally
        {
            if (connect != null && connect.State == System.Data.ConnectionState.Open)
                connect.Close();
        }
        return result;
    }
}